/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.prototype.console;

import java.io.File;
import java.util.ArrayList;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
/**
 *
 * @author ziboukhe
 */
public final class PoolThreadCmdLineRecover {
    /**.
     * local path of the application
     * @param appPath
     */
    private String appPath;
    /**.
     * @param folderName the folder name of all the project extracted from SF
     */
    private String folderName;
    /**.
     * @param url path of SF website to connect
     */
    private String url;
    /**.
     * @param tab contain all recovered links 
     */
    private ArrayList<String> tab;

    /**.
     * Constructor
     * @param newAppPath
     * @param newFolderName
     * @param url
     */
    public PoolThreadCmdLineRecover(final String newAppPath,
            final String newFolderName, final String url) {
        this.appPath = newAppPath;
        this.folderName = newFolderName;
        this.url = url;
        this.tab = new ArrayList<String>();
    }

    /**.
     * @param pageLink2
     * @throws InterruptedException
     */
    public void getProjects(final String pageLink2)
            throws InterruptedException {
        Connection connect;
        connect = new Connection(url);

        int numWorkerspr = connect.nbPage();
        int numWorkers;

        numWorkerspr--;
        ExecutorService pooldynamiquepr = Executors.newCachedThreadPool();

        JobThread[] workerspr = new JobThread[numWorkerspr];
        Future<ConcurrentHashMap<String, String>>[] futurespr
                = new Future[numWorkerspr];

        for (int i = 0; i < numWorkerspr; i++) {
            workerspr[i] = new JobThread(connect.urlPage().get(i));
            futurespr[i] = pooldynamiquepr.submit(workerspr[i]);
        }

        pooldynamiquepr.shutdown();

        for (int i = 0; i < numWorkerspr; i++) {
            try {
                connect.getConcurrentMap().putAll((futurespr[i].get()));
            } catch (Exception ex) {
                ex.getMessage();
            }
        }
        numWorkers = connect.getConcurrentMap().size();
        final int nbPage = 50;
        final int nbProjectByPage = 30;
        ExecutorService poolFixe = Executors.newFixedThreadPool(
                nbPage * nbProjectByPage);

        SvnJobThread[] workers = new SvnJobThread[numWorkers];
        Future<String>[] futures = new Future[numWorkers];

        int cpt = 0;
        for (ConcurrentHashMap.Entry<String, String> entry
            : connect.getConcurrentMap().entrySet()) {
            workers[cpt] =
                    new SvnJobThread(pageLink2 + entry.getValue());
            futures[cpt] = poolFixe.submit(workers[cpt]);
            cpt++;
        }

        poolFixe.shutdown();

        // recover command lines for SVN, CVS, GIT
        for (int i = 0; i < numWorkers; i++) {
            try {
                /**.
                 * Folder which contain all the projects
                 */
                File projectFolder = new File(appPath + "\\" + folderName);
                if (!projectFolder.exists()) {
                    projectFolder.mkdir();
                }
                /**.
                 * execute the command in a terminal
                 * to recover the differents projects
                 */

                tab.add(futures[i].get());

                Runtime.getRuntime()
                        .exec(futures[i].get(), null, projectFolder).waitFor();
                new JobGenerateStat(appPath + "\\statsvn.jar",
                        projectFolder.getPath() + "\\",
                        futures[i].get()).call();
            } catch (Exception ex) {
                ex.getMessage();
            }
        }
    }

    /**.
     * @return tab
     */
    public ArrayList<String> getTab() {
        return tab;
    }
}

